// GBReBar.cpp : implementation file
//

#include "stdafx.h"
#include "../GameBrowser.h"
#include "GBComboBoxEx.h"
#include "GBEditEx.h"
#include "GameBrowserTopPanel.h"


// CGBReBar

IMPLEMENT_DYNAMIC(CGBComboBoxEx, CComboBox)

CGBComboBoxEx::CGBComboBoxEx()
{
    m_bLButtonDown = FALSE;
    m_bMouseOver = FALSE;

    m_nButtonOffSet = ::GetSystemMetrics(SM_CXHTHUMB);
}

CGBComboBoxEx::~CGBComboBoxEx()
{
}


BEGIN_MESSAGE_MAP(CGBComboBoxEx, CComboBox)   
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_PAINT()
    ON_WM_MOUSEMOVE()
    ON_WM_MOUSELEAVE()	
	ON_CONTROL_REFLECT(CBN_EDITCHANGE, &CGBComboBoxEx::OnCbnEditchange)
	ON_WM_CREATE()
	ON_WM_DESTROY()
END_MESSAGE_MAP()


void CGBComboBoxEx::OnPaint()
{
    //Default();
    DrawComboBox();
}

void CGBComboBoxEx::DrawComboBox()
{
    CRect rcItem,rcBt;
    GetClientRect(&rcItem);

    rcBt.left = rcItem.right - m_nButtonOffSet - 2;
    rcBt.top = rcItem.top + 2;
    rcBt.bottom = rcItem.bottom - 2;
    rcBt.right = rcItem.right - 2;

    CPaintDC pDC(this);

    //CDC* pDC = GetDC();
    pDC.Draw3dRect(&rcItem,RGB(123,0,0),RGB(0,0,123));
    rcItem.DeflateRect(1,1);
    pDC.Draw3dRect(&rcItem,RGB(100,0,0),RGB(0,0,200));


    CDC memdc;
    CBitmap bt;

    memdc.CreateCompatibleDC(&pDC);
    if(m_bLButtonDown)
    {
        bt.LoadBitmap(IDB_COMBOBOX_MOUSEDOWN);
    }
    else
    {
        bt.LoadBitmap(IDB_COMBOBOX_NORMAL);
    }
    if(m_bMouseOver&&!m_bLButtonDown)
    {
        if(bt.m_hObject)
        {
            bt.DeleteObject();
        }
        bt.LoadBitmap(IDB_COMBOBOX_MOUSEOVER);
    }

    BITMAP bm;
    bt.GetObject(sizeof(bm),&bm);

    CBitmap* poldbmp=memdc.SelectObject(&bt);
    pDC.StretchBlt(rcBt.left,rcBt.top,2,rcBt.Height(),&memdc,0,0,2,bm.bmHeight,SRCCOPY);
    pDC.StretchBlt(rcBt.left+2,rcBt.top,rcBt.Width()-13,rcBt.Height(),&memdc,2,0,1,bm.bmHeight,SRCCOPY);
    pDC.StretchBlt(rcBt.right-13,rcBt.top,13,rcBt.Height(),&memdc,3,0,13,bm.bmHeight,SRCCOPY);
    memdc.SelectObject(poldbmp);

    

    //ReleaseDC(pDC);

}

void CGBComboBoxEx::OnLButtonDown( UINT nFlags, CPoint point )
{
    CRect rcItem,rcBt;
    GetClientRect(&rcItem);

    rcBt.left = rcItem.right - m_nButtonOffSet - 2;
    rcBt.top = rcItem.top + 2;
    rcBt.bottom = rcItem.bottom - 2;
    rcBt.right = rcItem.right - 2;
    if (rcBt.PtInRect(point))
    {
        m_bLButtonDown = TRUE;
        Invalidate();
    }

    CComboBox::OnLButtonDown(nFlags,point);
}

void CGBComboBoxEx::OnLButtonUp( UINT nFlags, CPoint point )
{
    m_bLButtonDown = FALSE;
    Invalidate();

    CComboBox::OnLButtonUp(nFlags,point);
}

void CGBComboBoxEx::OnMouseMove( UINT nFlags, CPoint point )
{
    if (!m_bMouseOver)
    {
        CRect rcItem,rcBt;
        GetClientRect(&rcItem);

        rcBt.left = rcItem.right - m_nButtonOffSet - 2;
        rcBt.top = rcItem.top + 2;
        rcBt.bottom = rcItem.bottom - 2;
        rcBt.right = rcItem.right - 2;

        if (rcBt.PtInRect(point))
        {
            m_bMouseOver = TRUE;
            InvalidateRect(NULL,FALSE);

            TRACKMOUSEEVENT tme;
            tme.cbSize = sizeof(TRACKMOUSEEVENT);
            tme.hwndTrack = m_hWnd;
            tme.dwFlags = TME_LEAVE | TME_HOVER;
            tme.dwHoverTime = 1;
            TrackMouseEvent(&tme);
        }


    }

    CComboBox::OnMouseMove(nFlags,point);
}

void CGBComboBoxEx::OnMouseLeave()
{
    m_bMouseOver = FALSE;
    InvalidateRect(NULL,FALSE);
    CComboBox::OnMouseLeave();
}


void CGBComboBoxEx::OnCbnEditchange()
{
	// TODO: Add your control notification handler code here
}

BOOL CGBComboBoxEx::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	return CComboBox::PreTranslateMessage(pMsg);
}

int CGBComboBoxEx::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CComboBox::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_hwndEdit = GetWindow(GW_CHILD);
	m_cusEditEx.SubclassWindow(m_hwndEdit->m_hWnd);
	m_cusEditEx.SetGBComboBox(this);

	return 0;
}

void CGBComboBoxEx::OnDestroy()
{
	if ( m_cusEditEx.GetSafeHwnd() != NULL )
	{
		m_cusEditEx.UnsubclassWindow();
	}

	CComboBox::OnDestroy();
}

CString CGBComboBoxEx::GetCurrentInputURL()
{
	CString strAdd;
	m_cusEditEx.GetWindowText(strAdd);

	return strAdd;
}

void CGBComboBoxEx::OnUserPutEnterKey()
{
	CWnd *pParanet = GetParent();

	CGameBrowserTopPanel *pTopPane = (CGameBrowserTopPanel *)pParanet;
	if (pTopPane != NULL)
	{
		pTopPane->OnUserInputEnter();
	}
}
